var h = acre.require("lib/helper/helpers.sjs");
var deferred = acre.require("lib/promise/deferred.sjs");

function handler() {
  var handler = {};

  // this part is no different than standard Mjt
  handler.to_js = acre.handlers.mjt.to_js;
  handler.to_module = acre.handlers.mjt.to_module;

  handler.to_http_response = function(module, script) {
    var d = deferred.all(module.c || {}, true)
      .then(function(c) {
        h.extend(module.c, c);
        return module;
      })
      .then(function(module) {
        var template = acre.require("page.mjt");
        return template.page.call(template, module);
      })
      .then(function(render_result) {
        module.body = acre.markup.stringify(render_result);
      });
      
    acre.async.wait_on_results();
    d.cleanup();
    return module;
  };

  return handler;
};
